﻿Imports DevComponents.DotNetBar
Imports quanlydulich.Helpers

Public Enum EnumUpdateMode
    UPDATE
    DELETE
    INSERT
End Enum


Public Class LAPPHIEUDATTOUR
    Inherits Office2007Form

    Friend WithEvents frmKhachHang As KHACHHANG
    Private Updated As Boolean = False
    Private UpdateMode As EnumUpdateMode
    ' Hidden files are used to store ids 
    Private txtMaPhieu As DevComponents.DotNetBar.Controls.TextBoxX
    Public MaTour As String
    Public TenTour As String

    Public Sub InitHiddenFields()
        txtMaPhieu = New DevComponents.DotNetBar.Controls.TextBoxX
        txtMaPhieu.Name = "txtMaChiTietPhieu"
        txtMaPhieu.Text = ""
        txtMaPhieu.Visible = False
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Close()
    End Sub

    Private Sub btnThemVaoDanhSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemVaoDanhSach.Click
        frmKhachHang = New KHACHHANG
        frmKhachHang.ShowDialog()
    End Sub

    Private Sub frmKhachHang_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmKhachHang.Handler
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        Dim objBus As KHACHHANGBUS = New KHACHHANGBUS(GlobalVars.g_strConnectionString)
        Dim lstItem As DataTable = objBus.SelectByID(Integer.Parse(comp(0))).Tables(0)

        Dim objListViewItem As ListViewItem = New ListViewItem
        objListViewItem.Text = lstItem.Rows(0)("ID").ToString()
        objListViewItem.SubItems.Add(lstItem.Rows(0)("TenKH").ToString())
        objListViewItem.SubItems.Add(IIf(UCase(lstItem.Rows(0)("GioiTinh").ToString()) = "TRUE", "Nam", "Nữ"))
        objListViewItem.SubItems.Add(lstItem.Rows(0)("CMND").ToString())
        objListViewItem.SubItems.Add(lstItem.Rows(0)("SoDT").ToString())
        objListViewItem.SubItems.Add(lstItem.Rows(0)("QuocTich").ToString())
        lstvDanhSach.Items.Add(objListViewItem)
    End Sub

    Private Sub btnXoaKhoiDanhSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaKhoiDanhSach.Click
        If lstvDanhSach.SelectedItems.Count > 0 Then
            lstvDanhSach.Items.RemoveAt(lstvDanhSach.SelectedIndices(0))
        End If
    End Sub

    Public Sub EraseTextboxes()
        lstvDanhSach.Items.Clear()
        txtHinhThucTT.Text = ""
    End Sub

    Public Sub SetDefaultMode(ByVal val As Boolean)
        txtHinhThucTT.ReadOnly = val
        btnThemVaoDanhSach.Enabled = Not val
        btnXoaKhoiDanhSach.Enabled = Not val

        btnThem.Enabled = val
        btnSua.Enabled = val
        btnXoa.Enabled = val
        btnGhi.Enabled = Not val
        btnHuy.Enabled = Not val
    End Sub

    Public Sub DisableCmdButtons()
        btnThem.Enabled = False
        btnSua.Enabled = False
        btnXoa.Enabled = False
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        UpdateMode = EnumUpdateMode.INSERT
        EraseTextboxes()
        SetDefaultMode(False)
        DisableCmdButtons()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If txtMaPhieu.Text <> "" Then
            UpdateMode = EnumUpdateMode.UPDATE
            SetDefaultMode(False)
            DisableCmdButtons()
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If lstvData.SelectedItems.Count > 0 Then
            If MessageBox.Show("Bạn có chắc chắn xóa dữ liệu này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                Dim objBus As PHIEUDATTOURBUS = New PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)
                If objBus.Delete(Integer.Parse(txtMaPhieu.Text)) Then
                    Updated = True
                    EraseTextboxes()
                    txtMaPhieu.Text = ""
                    LoadData()
                    MessageBoxEx.Show("Xóa dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Else
                    MessageBoxEx.Show("Xóa dữ liệu thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        End If
    End Sub

    Public Sub LoadData()
        Dim objBus As PHIEUDATTOURBUS = New PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)
        Dim lstItem As DataTable = objBus.SelectByIDTour(Integer.Parse(txtMaTour.Text)).Tables(0)
        lstvData.Items.Clear()
        If lstItem.Rows.Count > 0 Then
            For i As Integer = 0 To lstItem.Rows.Count - 1
                Dim objListViewItem As ListViewItem = New ListViewItem()
                objListViewItem.Tag = lstItem.Rows(i)("ID").ToString()
                objListViewItem.Text = (i + 1).ToString()
                objListViewItem.SubItems.Add(String.Format("{0:dd/MM/yyyy HH:mm:ss}", DateTime.Parse(lstItem.Rows(i)("NgayLapPhieu").ToString())))
                lstvData.Items.Add(objListViewItem)
            Next
        End If
    End Sub

    Private Sub btnGhi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGhi.Click
        Dim errorText As String = ""
        Dim success As Boolean = False

        If Not ValidateUserInput(errorText) Then
            MessageBoxEx.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        If UpdateMode = EnumUpdateMode.INSERT Then
            If ActionAdd() Then
                success = True
            End If
        ElseIf UpdateMode = EnumUpdateMode.UPDATE Then
            If ActionUpdate() Then
                success = True
            End If
        End If

        If success Then
            'LoadData()
            SetDefaultMode(True)
            Updated = True
            MessageBoxEx.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBoxEx.Show("Cập nhật dữ liệu thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Public Function ActionAdd() As Boolean
        Try
            Dim item As PHIEUDATTOURDTO = New PHIEUDATTOURDTO
            item.IDNhanVien = GlobalVars.g_CurrentUser.ID
            item.IDTour = Integer.Parse(txtMaTour.Text)
            item.HinhThucThanhToan = txtHinhThucTT.Text
            item.NgayLapPhieu = DateTime.Now

            Dim objBus As PHIEUDATTOURBUS = New PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)

            If objBus.Insert(item) Then
                Dim objChiTietBus As CHITIET_PHIEUDATTOURBUS = New CHITIET_PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)
                For i As Integer = 0 To lstvDanhSach.Items.Count - 1
                    Dim chitiet As CHITIET_PHIEUDATTOURDTO = New CHITIET_PHIEUDATTOURDTO
                    chitiet.IDPhieuDatTour = item.ID
                    chitiet.IDKhachHang = lstvDanhSach.Items(i).SubItems(0).Text
                    objChiTietBus.Insert(chitiet)
                Next
            End If

            Return True
        Catch
            Return False
        End Try
    End Function

    Public Function ActionUpdate() As Boolean
        Try
            Dim objBus As PHIEUDATTOURBUS = New PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)
            Dim lstItem As DataTable = objBus.SelectByID(Integer.Parse(txtMaPhieu.Text)).Tables(0)

            Dim item As PHIEUDATTOURDTO = New PHIEUDATTOURDTO
            item.ID = Integer.Parse(txtMaPhieu.Text)
            item.IDNhanVien = lstItem.Rows(0)("IDNhanVien").ToString()
            item.IDTour = Integer.Parse(txtMaTour.Text)
            item.HinhThucThanhToan = txtHinhThucTT.Text
            item.NgayLapPhieu = DateTime.Parse(lstItem.Rows(0)("NgayLapPhieu").ToString())
            objBus = New PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)

            If objBus.Update(item) Then
                Dim objChiTietBus As CHITIET_PHIEUDATTOURBUS = New CHITIET_PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)
                If objChiTietBus.DeleteByIDPhieuDatTour(Integer.Parse(txtMaPhieu.Text)) Then
                    For i As Integer = 0 To lstvDanhSach.Items.Count - 1
                        objChiTietBus = New CHITIET_PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)
                        Dim chitiet As CHITIET_PHIEUDATTOURDTO = New CHITIET_PHIEUDATTOURDTO
                        chitiet.IDPhieuDatTour = Integer.Parse(txtMaPhieu.Text)
                        chitiet.IDKhachHang = lstvDanhSach.Items(i).SubItems(0).Text
                        objChiTietBus.Insert(chitiet)
                    Next
                End If
            End If

            Return True
        Catch
            Return False
        End Try
    End Function

    Public Function ValidateUserInput(ByRef errorText As String) As Boolean
        If lstvDanhSach.Items.Count = 0 Then
            errorText = "Danh sách rỗng!"
            Return False
        End If
        Return True
    End Function

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        If Not txtMaPhieu.Text = "" Then
            LoadCurrentQuaTrinhInfo(Integer.Parse(txtMaPhieu.Text))
        Else
            EraseTextboxes()
        End If
        SetDefaultMode(True)
    End Sub

    Public Sub LoadCurrentQuaTrinhInfo(ByVal id As Integer)
        Dim objBus As PHIEUDATTOURBUS = New PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)
        Dim phieudattour As DataTable = objBus.SelectByID(id).Tables(0)
        txtHinhThucTT.Text = phieudattour.Rows(0)("HinhThucThanhToan").ToString()
        txtNgayLapPhieu.Text = String.Format("{0:dd/MM/yyyy}", DateTime.Parse(phieudattour.Rows(0)("NgayLapPhieu").ToString()))

        Dim objChiTietBus As CHITIET_PHIEUDATTOURBUS = New CHITIET_PHIEUDATTOURBUS(GlobalVars.g_strConnectionString)
        Dim chitiet As DataTable = objChiTietBus.SelectByIDPhieuDatTour(id).Tables(0)
        lstvDanhSach.Items.Clear()
        If chitiet.Rows.Count > 0 Then
            For i As Integer = 0 To chitiet.Rows.Count - 1
                Dim objListViewItem As ListViewItem = New ListViewItem
                objListViewItem.Text = chitiet.Rows(i)("IDKhachHang").ToString()
                objListViewItem.SubItems.Add(chitiet.Rows(i)("TenKH").ToString())
                objListViewItem.SubItems.Add(IIf(UCase(chitiet.Rows(i)("GioiTinh").ToString()) = "TRUE", "Nam", "Nữ"))
                objListViewItem.SubItems.Add(chitiet.Rows(i)("CMND").ToString())
                objListViewItem.SubItems.Add(chitiet.Rows(i)("SoDT").ToString())
                objListViewItem.SubItems.Add(chitiet.Rows(i)("QuocTich").ToString())
                lstvDanhSach.Items.Add(objListViewItem)
            Next
        End If
    End Sub

    Private Sub btnThoat_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

    Private Sub lstvData_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstvData.SelectedIndexChanged
        If lstvData.SelectedItems.Count > 0 Then
            txtMaPhieu.Text = lstvData.SelectedItems(0).Tag.ToString()
            LoadCurrentQuaTrinhInfo(Integer.Parse(txtMaPhieu.Text))
        End If
    End Sub

    Private Sub LapPhieuDatTour_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitHiddenFields()
        txtMaTour.Text = MaTour
        txtTenTour.Text = TenTour
        LoadData()
        txtNgayLapPhieu.Text = String.Format("{0:dd/MM/yyyy}", DateTime.Now)
    End Sub
End Class